package gold.digger;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC386 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public List<Integer> lexicalOrder(int n) {
            List<Integer> ans = new ArrayList<>();
            for (int i = 1; i <= 9; i++) {
                dfs(i, n, ans);
            }

            return ans;
        }

        public void dfs(int cur, int n, List<Integer> ans) {
            if (cur > n) return;
            ans.add(cur);
            for (int i = 0; i <= 9; i++) {
                dfs(cur * 10 + i, n, ans);
            }
        }
    }

    class Solution_Old {
        public List<Integer> lexicalOrder(int n) {
            TreeSet<Integer> set = new TreeSet<>(new Comparator<Integer>() {
                @Override
                public int compare(Integer o1, Integer o2) {
                    return String.valueOf(o1).compareTo(String.valueOf(o2));
                }
            });

            for (int i = 1; i <= n; i++) {
                set.add(i);
            }

            return new ArrayList<>(set);
        }
    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.lexicalOrder(13));
    }

    public static void main(String[] args) throws Exception {
        LC386 an = new LC386();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
